#!/usr/bin/env python
#coding=utf-8

import wx
from panel import Panel

class InputPanel(Panel):
    ''' Representation of the input panel. '''

    def __init__(self, parent, config):
        Panel.__init__(self, parent, -1, wx.DefaultPosition,
                                    wx.DefaultSize, wx.NB_TOP|wx.TAB_TRAVERSAL)
        self.create_sizer()

        self.create_lang1_input_field(config.get('Settings','lang1'))
        self.create_lang2_input_field(config.get('Settings','lang2'))
        self.create_category_fields()
        self.create_radio_box()
        self.create_buttons()
        self.SetSizer(self.frameSizer)

    def clear_lang2_input(self):
        '''Clear the english input field. '''

        self.lang2Input.Clear()

    def clear_lang1_input(self):
        '''Clear the german input field. '''

        self.lang1Input.Clear()

    def create_buttons(self):
        ''' Create save and cancel'''

        self.cancelBtn = wx.Button(self,id=-1,label='Cancel')
        self.actionBox.Add(self.cancelBtn)

        self.submitBtn = wx.Button(self,id=-1,label='Save')
        self.actionBox.Add(self.submitBtn)

    def create_category_fields(self):
        ''' Create Category text and category list. '''

        categoryTxt = wx.StaticText(self, -1, label='Category')
        self.leftBox.Add(categoryTxt)

        self.categoryLst = wx.ListBox(self,-1,
            size = wx.Size(200,450), style=wx.LB_MULTIPLE, choices = [])
        self.leftBox.Add(self.categoryLst)

    def create_lang1_input_field(self, lang):
        ''' Create text and lang1Input field. '''

        lang1Txt = wx.StaticText(self, -1, label=lang)
        self.inputBox.Add(lang1Txt,flag=wx.TOP,border=20)
        self.lang1Input = wx.TextCtrl(self, -1, size = wx.Size(200,25))
        self.inputBox.Add(self.lang1Input, flag=wx.TOP,border=20)

    def create_lang2_input_field(self, lang):
        ''' Create text and lang2Input field. '''

        lang2Txt = wx.StaticText(self, -1, label=lang)
        self.inputBox.Add(lang2Txt)

        self.lang2Input = wx.TextCtrl(self, -1, size = wx.Size(200,25))
        self.inputBox.Add(self.lang2Input)

    def create_radio_box(self):
        ''' Create radio box for selecting btw update error table or not. '''

        questionTxt = wx.StaticText(self, -1, label='Save in error table? ')
        self.questionBox.Add(questionTxt)
        self.yesBtn = wx.RadioButton(self, -1, label='Yes', style=wx.RB_GROUP)
        self.questionBox.Add(self.yesBtn)
        self.noBtn = wx.RadioButton(self, -1, label='No')
        self.questionBox.Add(self.noBtn)

    def create_sizer(self):
        ''' Create all necessary sizers. '''

        self.frameSizer = wx.BoxSizer(orient=wx.HORIZONTAL)
        self.leftBox = wx.BoxSizer(orient=wx.VERTICAL)
        self.rightBox = wx.BoxSizer(orient=wx.VERTICAL)
        self.inputBox = wx.FlexGridSizer(rows=2,cols=2,vgap=2,hgap=2)
        self.questionBox = wx.BoxSizer(orient= wx.HORIZONTAL)
        self.actionBox = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.frameSizer.Add(self.leftBox, flag=wx.LEFT|wx.TOP,border=10)
        self.frameSizer.Add(self.rightBox, flag=wx.LEFT|wx.TOP,border=10)
        self.rightBox.Add(self.inputBox,flag=wx.LEFT,border=20)
        self.rightBox.Add(self.questionBox,flag=wx.LEFT|wx.TOP,border=20)
        self.rightBox.Add(self.actionBox,flag=wx.LEFT|wx.TOP,border=20)

    def deselect_categories(self):
        ''' Before updating te catgories they should be deselected. '''

        self.categoryLst.DeselectAll()

    def get_lang1_input(self):
        ''' Returns the string from lang1Input. '''

        return self.lang1Input.GetValue()

    def get_lang2_input(self):
        ''' Returns the string from lang2Input. '''

        return self.lang2Input.GetValue()

    def get_submit_btn_label(self):
        ''' Returned value could be: Save or Update. '''

        return self.submitBtn.GetLabel()

    def get_selected_categories(self):
        ''' Returns the selected categories. '''

        resultSet = set()
        selection = self.categoryLst.GetSelections()
        for idx in selection:
            resultSet.add(self.categoryLst.GetString(idx))
        return resultSet

    def get_yesBtn(self):
        ''' Return the value of yesBtn. '''

        return self.yesBtn.GetValue()

    def set_categories(self, categories):
        ''' Fill the categoryLst. '''

        self.Freeze()
        self.categoryLst.Set(categories)
        self.Thaw()

    def select_category_lst_item(self, category):
        ''' Select one category from the list. '''

        self.categoryLst.SetStringSelection(category)

    def set_focus(self):
        '''Set focus on the input field.'''

        wx.CallAfter(self.lang1Input.SetFocus)

    def set_category_by_idx(self, idx):
        ''' Set category by index. '''

        self.categoryLst.SetSelection(idx)
        self.set_category_by_name('new')

    def set_category_by_name(self, name):
        ''' Set category by name - like traffic. '''

        self.categoryLst.SetStringSelection(name)

    def set_lang1_input(self, text):
        ''' Write lang1 text for edit into input field. '''

        self.lang1Input.SetValue(text)

    def set_lang2_input(self, text):
        ''' Write german text for edit into input field. '''

        self.lang2Input.SetValue(text)

    def set_noBtn(self, value):
        ''' Set the yes|no option box. '''

        self.noBtn.SetValue(value)

    def set_submit_btn_label(self, label):
        ''' Allowed labels are: Save, Update  '''

        self.submitBtn.SetLabel(label)

    def set_yesBtn(self, value):
        ''' Set the yes|no option box. '''

        self.yesBtn.SetValue(value)
